{% extends 'base.html' %}
{% block title %}
    修改用户信息
{% endblock %}

{% block content %}
    {% if user.is_superuser is True %}
        <form class="layui-form" action="" method="post">
            <div class="layui-form-item">
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-block">
                    <input type="text" name="username" required lay-verify="required" placeholder="请输入用户名"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-inline">
                    <input type="password" name="password1" lay-verify="pass" placeholder="请输入密码"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux"></div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">确认密码</label>
                <div class="layui-input-inline">
                    <input type="password" name="password2" lay-verify="pass" placeholder="请输入密码"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux"></div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">邮箱</label>
                <div class="layui-input-block">
                    <input type="text" name="email" required lay-verify="required|email" placeholder="请输入邮箱"
                           autocomplete="off" class="layui-input">
                    {% csrf_token %}
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">是否超级管理员</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="is_superuser" lay-skin="switch" lay-text="True|False">
                </div>
            </div>

            {% for redis in rediss %}
                <div class="layui-form-item">
                    <label class="layui-form-label">{{ redis.name }}</label>
                    <div class="layui-input-block">
                        <select name="{{ redis.name }}">
                            {% for a in auth %}
                                {% if a.redis == redis.id %}
                                    <option value="{{ a.pre_auth }}">{% if a.pre_auth == 1 %}
                                        查看{% elif a.pre_auth == 2 %}编辑{% elif a.pre_auth == 3 %}
                                        删除{% endif %}</option>
                                {% endif %}
                            {% endfor %}
                            <option value="">无权限</option>
                            <option value="1">查看</option>
                            <option value="2">编辑</option>
                            <option value="3">删除</option>
                        </select>
                    </div>
                </div>
            {% endfor %}
            {% for cluster in clusters %}
                <div class="layui-form-item">
                    <label class="layui-form-label">{{ cluster.name }}</label>
                    <div class="layui-input-block">
                        <select name="{{ cluster.name }}">
                            {% for a in auth %}
                                {% if a.redis == cluster.id %}
                                    <option value="{{ a.pre_auth }}">{% if a.pre_auth == 1 %}
                                        查看{% elif a.pre_auth == 2 %}编辑{% elif a.pre_auth == 3 %}
                                        删除{% endif %}</option>
                                {% endif %}
                            {% endfor %}
                            <option value="">无权限</option>
                            <option value="1">查看</option>
                            <option value="2">编辑</option>
                            <option value="3">删除</option>
                        </select>
                    </div>
                </div>
            {% endfor %}

            {% if user_error %}
                <div class="layui-form-item">
                    <div>
                        <label class="layui-input-block">提示：{{ user_error }}</label>
                    </div>
                </div>
            {% endif %}
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                    {#      <button type="reset" class="layui-btn layui-btn-primary">重置</button>#}
                </div>
            </div>
        </form>

        <script>
            //Demo
            layui.use('form', function () {
                var form = layui.form;

                form.verify({
                    username: function (value, item) { //value：表单的值、item：表单的DOM对象
                        if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                            return '用户名不能有特殊字符';
                        }
                        if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                            return '用户名首尾不能出现下划线\'_\'';
                        }
                        if (/^\d+\d+\d$/.test(value)) {
                            return '用户名不能全为数字';
                        }
                    }

                    //我们既支持上述函数式的方式，也支持下述数组的形式
                    //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
                    , pass: [
                        /^[\S]{6,12}$/
                        , '密码必须6到12位，且不能出现空格'
                    ]
                });

                //监听提交
                form.on('submit(formDemo)', function (data) {
                    {#    layer.msg(JSON.stringify(data.field));#}
                    return true;
                });
            });
        </script>
    {% else %}
        <script>
            layui.use('layer', function () {
                var layer = layui.layer;
                layer.confirm('您没有权限，请联系管理员授权。', {
                    btn: ['确认']
                }, function () {
                    window.location.href = "/";
                })
            });
        </script>
    {% endif %}
{% endblock %}